package com.example.studentsRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studentsRoom.entity.Building;
import com.example.studentsRoom.entity.Dormitory;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.mapper.BuildingMapper;
import com.example.studentsRoom.mapper.DormitoryMapper;
import com.example.studentsRoom.service.IDormitoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentsRoom.util.DormitoryUtil;
import com.example.studentsRoom.util.ResultUtil;
import com.example.studentsRoom.vo.BuildAndDormitoryVo;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@Service
public class DormitoryService extends ServiceImpl<DormitoryMapper, Dormitory> implements IDormitoryService {

    @Autowired
    DormitoryMapper dormitoryMapper;

    @Autowired
    BuildingMapper buildingMapper;

    @Autowired
    BuildingService buildingService;

    /**
     * 查询可用宿舍的信息
     * @return
     */
    @Override
    public ResultVo availableList(Integer bid) {
        QueryWrapper<Dormitory> dormitoryQueryWrapper=new QueryWrapper<>();

        //获取到目标宿舍数据
        dormitoryQueryWrapper.eq("b_id",bid);

        List<Dormitory> dormitories = dormitoryMapper.selectList(dormitoryQueryWrapper);

        //实例化工具类
        List<DormitoryUtil> dormitoryUtils=new ArrayList<>();
        //遍历结果
        for (Dormitory dormitory : dormitories) {

            DormitoryUtil dormitoryUtil=new DormitoryUtil();

            //如果宿舍没有床位
            if (Integer.parseInt(dormitory.getDorBed())==0){
                BeanUtils.copyProperties(dormitory,dormitoryUtil);
                //设置ture
                dormitoryUtil.setDisabled(true);
            }
            BeanUtils.copyProperties(dormitory,dormitoryUtil);
            dormitoryUtils.add(dormitoryUtil);
        }

        return ResultUtil.success(dormitoryUtils);
    }

    /**
     * 分页查询宿舍
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo listPage(Integer page, Integer size) {
        Page<Dormitory> dormitoryPage = new Page<>(page, size);

        Page<Dormitory> resultPage = this.dormitoryMapper.selectPage(dormitoryPage, null);

        PageVo pageVo = BuildAndDormitoryVo.getBuild(resultPage, buildingMapper);
        return pageVo;
    }

    /**
     * 模糊查询+分页
     *
     * @param searchForm
     * @return
     */
    public PageVo search(SearchForm searchForm) {
        Page<Dormitory> dormitoryPage = new Page<>(searchForm.getPage(), searchForm.getSize());

        //模糊查询加分页
        Page<Dormitory> resultPage = null;

        //判断value值是不是null
        if (searchForm.getValue().equals("")) {
            resultPage = this.dormitoryMapper.selectPage(dormitoryPage, null);
        } else {
            if (searchForm.getKey().equals("dor_name")){
                QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
                queryWrapper.like(searchForm.getKey(), searchForm.getValue());
                resultPage = this.dormitoryMapper.selectPage(dormitoryPage, queryWrapper);
            }else{

                SearchForm bid = buildingService.getBid(searchForm);

                QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
                queryWrapper.like(searchForm.getKey(), bid.getValue());
                resultPage = this.dormitoryMapper.selectPage(dormitoryPage, queryWrapper);
            }
        }

        PageVo pageVo = BuildAndDormitoryVo.getBuild(resultPage, buildingMapper);
        return pageVo;
    }
}
